package com.hmkj.core.service.account.impl;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.mapper.account.AccountLogSimulationMapper;
import com.hmkj.core.mapper.account.AccountSimulationMapper;
import com.hmkj.core.po.account.AccountLogSimulation;
import com.hmkj.core.po.account.AccountSimulation;
import com.hmkj.core.service.account.AccountSimulationService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 模拟账户业务相关Service接口实现<br>
 *
 * @author eproo
 * @ClassName: AccountSimulationServiceImpl
 * @date 2018-07-05 09:50:54
 */
@Service
@Scope("prototype")
@Transactional(rollbackFor = Exception.class)
public class AccountSimulationServiceImpl implements AccountSimulationService {

    @Resource
    private AccountSimulationMapper accountSimulationMapper;
    @Resource
    private AccountLogSimulationMapper accountLogSimulationMapper;

    @Override
    public GenericMapper<AccountSimulation, Integer> _getMapper() {
        return accountSimulationMapper;
    }

    @Override
    public AccountSimulation selectByUID(Integer userId) {
        return accountSimulationMapper.selectByUID(userId);
    }

    @Override
    public void handEditAccount(AccountSimulation accountSimulation, BigDecimal amount) {
        AccountLogSimulation.TYPE type = null;
        String remark = "";
        if (amount.compareTo(BigDecimal.ZERO) == 1) {
            type = AccountLogSimulation.TYPE.T1;
            remark = "手动入金" + amount + "元";
        } else {
            type = AccountLogSimulation.TYPE.T2;
            remark = "手动出金" + amount.negate() + "元";
        }
        //手动出入金
        AccountLogSimulation accountLogSimulation = AccountLogSimulation.buildLog(accountSimulation.getUserId(), 0, amount, type, accountSimulation, remark);
        accountLogSimulationMapper.insert(accountLogSimulation);

        //更新资金
        accountSimulationMapper.updateAccountSimulate(amount, amount, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, accountSimulation.getUserId());
    }
}
